/**
 * @asignatura: Procesadores de Lenguajes.
 * @autor:      Anthony Vittorio Russo Cabrera
 * @curso:      2012/2013
 * @practica:   06 - ANTLR. Ambigüedades Sintácticas
 * @descripcion:
 */
package pl.p06;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;

import pl.p06.antlr3.ANTLRv3Lexer;
import pl.p06.antlr3.ANTLRv3Parser;
import pl.p06.antlr3.CLexer;
import pl.p06.antlr3.CParser;
import pl.p06.antlr3.GDLLexer;
import pl.p06.antlr3.GDLParser;
import pl.p06.antlr3.XMLLexer;
import pl.p06.antlr3.XMLParser;
import pl.p06.case_insensitivity.ANTLRFileStreamCaseInsensitivity;
import pl.p06.case_insensitivity.ANTLRInputStreamCaseInsensitivity;

public class Procesador {
  
  public static void lexerC(ANTLRStringStream input) {
    CLexer lexer = new CLexer(input);
    Token token;
    for (token = lexer.nextToken(); token.getType() != Token.EOF; token = lexer.nextToken()) {
      System.out.println(token);
    }// for
  }// lexerYACC
  
  public static void runC(ANTLRStringStream input) throws RecognitionException {
    CLexer lexer = new CLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CParser parser = new CParser(tokens);
    parser.rulesC();
  }// runC
  public static void runANTLRv3(ANTLRStringStream input) throws RecognitionException {
    ANTLRv3Lexer lexer = new ANTLRv3Lexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ANTLRv3Parser parser = new ANTLRv3Parser(tokens);
    parser.grammarDef();
  }// runANTLRv3
  public static void runXML(ANTLRStringStream input) throws RecognitionException {
    XMLLexer lexer = new XMLLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    XMLParser parser = new XMLParser(tokens);
    parser.document();
  }// runXML
  public static void runGDL(ANTLRStringStream input) throws RecognitionException {
    GDLLexer lexer = new GDLLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    GDLParser parser = new GDLParser(tokens);
    parser.graph();
  }// runGDL

  public static void main(String args[]) {
    // Determinar parámetros
    ANTLRStringStream input = null;
    try {
      switch (args.length) {
        case 1: // InputStream | FileStream
          int option = -1;
          try {
            option = Integer.parseInt(args[0]);
            input = new ANTLRInputStreamCaseInsensitivity(System.in);
            switch (option) {
              case 0: // C
                runC(input);
                break;
              case 1: // ANTLRv3
                runANTLRv3(input);
                break;
              case 2: // XML
                runXML(input);
                break;
              case 3: // GDL
                runGDL(input);
                break;
              default:
                System.err.println("Opción incorrecta: " + option);
                System.exit(0);
            }// switch
          } catch (NumberFormatException e) {
            // args[0] no es un número
            input = new ANTLRFileStreamCaseInsensitivity(args[0]);
            if (args[0].matches(".*c$")) {
              // C
              runC(input);
            } else if (args[0].matches(".*g$")) {
              // ANTLRv3
              runANTLRv3(input);
            } else if (args[0].matches(".*xml$")) {
              // XML
              runXML(input);
            } else if (args[0].matches(".*gdl")) {
              // GDL
              runGDL(input);
            } else {
              System.err.println("fichero con extensión incorrecta: " + args[0]);
              System.exit(0);
            }// if-else
          }// try-catch
          break;
        default:
          System.err.println("java Procesador (option | file)");
          System.exit(0);
      }// switch

    // Execpciones
    } catch (FileNotFoundException e) {
      System.err.println("File Not Found: \"" + args[0] + "\"");
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Input/Output Error");
      e.printStackTrace();
    } catch (RecognitionException e) {
      System.err.println("Parser Error");
      e.printStackTrace();
    }// try-catch
  } // main

}// Procesador